<?php

namespace Estore\Model;

//use Zend\Db\Adapter\Adapter;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
use Zend\Db\Sql\Select;
//use Zend\XmlRpc\Value\String;
use Zend\Paginator\Adapter\DbSelect;
use Zend\Paginator\Paginator;
use Estore\Model\Storeorders;

class StoreordersTable 
{
	protected $tableGateway;
	
	public function __construct(TableGateway $tableGateway)
	{
		$this->tableGateway = $tableGateway;
		//$this->uploadSharingTableGateway = $uploadSharingTableGateway;
	}
	public function saveStoreorders(Storeorders $Store_order)
	{
		$data = array(
				'store_product_id' => $Store_order->store_product_id,
				'qty' => $Store_order->qty,
				'total' => $Store_order->total,
				'status' => $Store_order->status,
				'stamp' => $Store_order->stamp,
				'first_name' => $Store_order->first_name,
				'last_name' => $Store_order->last_name,
				'email' => $Store_order->email,
				'ship_to_street' => $Store_order->ship_to_street,
				'ship_to_city' => $Store_order->ship_to_city,
				'ship_to_state' => $Store_order->ship_to_state,
				'ship_to_zip' => $Store_order->ship_to_zip,
		);
		$id = (int)$Store_order->id;
		if ($id == 0) {
			$this->tableGateway->insert($data);
		} else {
			if ($this->getStoreorders($id)) {
				$this->tableGateway->update($data, array('id' => $id));
			} else {
				throw new \Exception('Product ID does not exist');
			}
		}
	}
	
	public function getStoreorders($id)
	{
		$id = (int) $id;
		$rowset = $this->tableGateway->select(array('id' => $id));
		$row = $rowset->current();
		if (!$row) {
			throw new \Exception("Could not find row $id");
		}
		return $row;
	}
	
	/*public function getlastStore_products()
	{
		//$rowset = array();
		$rowset = $this->tableGateway->getLastInsertValue();
		//throw new \Zend\File\Transfer\Exception\InvalidArgumentException('  '.var_dump($rowset));
		return $rowset;
	}
	
	public function get($getStore_products.....)
	{
		$name = (string)$filename;
		//throw new \Exception("Could not find row $name");
		$rowset = $this->tableGateway->select(array('filename' => $name));
		$row = $rowset->current();
		//throw new \Exception("Could not find row $rowset");
		if (!$row) {
			//throw new \Exception("Could not find row $row");
			return NULL;
		}
		return $row;
	}*/
	
	//public function fetchAll()
	//{
	//	$resultSet = $this->tableGateway->select();
	//	return $resultSet;
	//}
	
	public function fetchAll($paginated=false)
	{
	
		if ($paginated) {
			// create a new Select object for the table products
			$select = new Select('store_orders');
			// create a new result set based on the product entity
			$resultSetPrototype = new ResultSet();
			$resultSetPrototype->setArrayObjectPrototype(new Storeorders());
			// create a new pagination adapter object
			$paginatorAdapter = new DbSelect(
					// our configured select object
					$select,
					// the adapter to run it against
					$this->tableGateway->getAdapter(),
					// the result set to hydrate
					$resultSetPrototype
			);
			$paginator = new Paginator($paginatorAdapter);
			//throw new \Exception("$paginator");
			return $paginator;
		}
		$resultSet = $this->tableGateway->select();
		return $resultSet;
	}
	
	public function deleteStore_products($id)
	{
		$this->tableGateway->delete(array('id' => $id));
	}
	
	public function getlastorder()
	{
		//$rowset = array();
		$rowset = $this->tableGateway->getLastInsertValue();
		//throw new \Zend\File\Transfer\Exception\InvalidArgumentException('  '.var_dump($rowset));
		return $rowset;
	}
	
}

?>

